package com.thunbu.thunbu_meeting_system.viewMdel

import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.ruffian.library.widget.REditText
import com.thunbu.lib_common.base.CoroutineViewModel
import com.thunbu.lib_common.config.BaseResponse
import com.thunbu.thunbu_meeting_system.await
import com.thunbu.thunbu_meeting_system.bean.AssetModel
import com.thunbu.thunbu_meeting_system.launch
import com.thunbu.thunbu_meeting_system.service.ApiService
import com.thunbu.thunbu_meeting_system.service.MainNet
import com.thunbu.thunbu_meeting_system.toRequestBody
import retrofit2.Call

class InventoryDetailVM : CoroutineViewModel<Unit>() {
    val detail = MutableLiveData<AssetModel>()
    val updateResult = MutableLiveData<Boolean>()

    override fun initModel() {

    }

    fun requestDetailInfo(mId: String?,from_assets:Boolean?) {
        launch({
            var result = if(from_assets == true){
                MainNet.server(ApiService::class.java).queryTaskDetail2(mId!!).await(true,true)
            }else{
                MainNet.server(ApiService::class.java).queryTaskDetail(mId!!).await(true,true)
            }
            detail.value = result.data
        })
    }

    fun updateDetail(
            mId: String?,
        assetStatus: Int, // 1 盘存; 2 盘亏
        etRemark: String?,
        etZc: String?,
        etBrand: String?,
        etModel: String?,
        user: String?,
        department: String?,
        address: String?,
        shuliang:String?,
    ) {
        val map = mutableMapOf<String, Any?>(
                "assetStatus" to assetStatus,
                "id" to mId
        )
        var isChange = false
        if(!etRemark.isNullOrEmpty()){
            isChange = true
            map["assetRemark"] = etRemark
        }
        if(!etZc.isNullOrEmpty()){
            isChange = true
            map["zichanmc"] = etZc
        }
        if(!etBrand.isNullOrEmpty()){
            isChange = true
            map["pinpai"] = etBrand
        }
        if(!etModel.isNullOrEmpty()){
            isChange = true
            map["guigexh"] = etModel
        }
        if(!user.isNullOrEmpty()){
            isChange = true
            map["shiyongrid"] = user
        }
        if(!department.isNullOrEmpty()){
            isChange = true
            map["shiyongglbmid"] = department
        }
        if(!address.isNullOrEmpty()){
            isChange = true
            map["cunfangddid"] = address
        }
        if(!shuliang.isNullOrEmpty()){
            isChange = true
            map["shuliang"] = shuliang
        }
        if(!isChange){
            ToastUtils.show("请填写好备注进行确认")
            return
        }
        launch({
            val result = MainNet.server(ApiService::class.java).updateTaskDetail(map.toRequestBody()).await(true,true)
            updateResult.value = true
        })

    }
}